import torch
import numpy as np
import random

def get_accurary(pred, label, device,threshold):
    # 初始化一个字典来存储每个类别的准确率  
    accuracies = {}  

    # 遍历每个类别  
    for key in pred: 
        # 使用阈值（如0.5）将预测概率转换为0或1  
        predictions = (pred[key] > threshold).float()  
        # 计算准确率  
        # print(predictions)
        # print(torch.tensor([[float(i)] for i in label[key]]).cuda())
        accuracy = (predictions == torch.tensor([[float(i)] for i in label[key]]).cuda()).float().mean().item()  
        # 存储每个类别的准确率 
        accuracies[key] = accuracy  
    # 计算平均准确率（微平均，因为每个样本对总体准确率的贡献相同）  
    average_accuracy = sum(accuracies.values()) / len(accuracies)  
    
    # 打印结果  
    # for key, acc in accuracies.items():  
    #     print(f"{key} Accuracy: {acc:.4f}")  
    # print(f"Average Accuracy: {average_accuracy:.4f}")  
    return average_accuracy

def set_seed(seed):
    torch.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
